﻿using AutoMapper;
using FoodModel;
using J.FoodManageSet.Application.Command.DishesCommand;
using J.FoodRepository.Impl;
using J.FoodRepository.InterFace;
using MediatR;

namespace J.FoodManageSet.Application.CommandHandler.DishesCommandHandler
{
    /// <summary>
    /// 菜品添加或修改
    /// </summary>
    public class DishesAddUpDateCommandHandler : IRequestHandler<DishesAddUpDateCommand, int>
    {
        private readonly IDishesReposittory _dishesReposittory;
        private readonly IMapper _mapper;

        public DishesAddUpDateCommandHandler(IDishesReposittory dishesReposittory,IMapper mapper)
        {
            _dishesReposittory = dishesReposittory;
            _mapper = mapper;
        }
        public async Task<int> Handle(DishesAddUpDateCommand request, CancellationToken cancellationToken)
        {
           
            int count = 0;
            if (request.DishId == 0)
            {
                DishesModel model = _mapper.Map<DishesModel>(request);
                count = await _dishesReposittory.DishesAdd(model);
            }
            else
            {
                DishesModel model = _mapper.Map<DishesModel>(request);
                count = await _dishesReposittory.DishesUpDate(model);
            }
            return count;
        }
    }
}
